package com.jiangyao.controller;

import com.jiangyao.common.ApiResult;
import com.jiangyao.model.dto.backend.QuestionDTO;
import com.jiangyao.model.dto.backend.QuestionDetailDTO;
import com.jiangyao.model.dto.backend.QuestionItemDelDTO;
import com.jiangyao.model.dto.backend.QuestionListPageDTO;
import com.jiangyao.model.vo.backend.QuestionDetailVO;
import com.jiangyao.model.vo.backend.QuestionListVO;
import com.jiangyao.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 试题 前端控制器
 * </p>
 *
 * @author yangyongchuan
 * @since 2020-08-20
 */
@RestController
@RequestMapping("/question")
@Api(tags = "试题管理")
public class QuestionController extends BaseController {

	@Autowired
	private QuestionService questionService;

	/**
	 * 获取试题列表
	 *
	 * @param listPageDTO 列表DTO
	 * @return 结果
	 */
	@ApiOperation(value = "获取试题列表")
	@PostMapping("/list")
	public ApiResult<QuestionListVO> getListPage(@Validated({QuestionListPageDTO.ListPage.class}) @RequestBody QuestionListPageDTO listPageDTO) {
		return ApiResult.success(questionService.getListPage(listPageDTO));
	}

	/**
	 * 增加试题
	 *
	 * @param questionDTO 增加DTO
	 * @return 结果
	 */
	@ApiOperation(value = "增加试题")
	@PostMapping("/add")
	public ApiResult<String> add(@Validated({QuestionDTO.Add.class}) @RequestBody QuestionDTO questionDTO) {
		questionService.add(questionDTO);
		return new ApiResult<>("ok");
	}

	/**
	 * 编辑试题
	 *
	 * @param questionDTO 编辑DTO
	 * @return 结果
	 */
	@ApiOperation(value = "编辑试题")
	@PostMapping("/edit")
	public ApiResult<String> edit(@Validated({QuestionDTO.Edit.class}) @RequestBody QuestionDTO questionDTO) {
		questionService.edit(questionDTO);
		return new ApiResult<>("ok");
	}

	/**
	 * 编辑试题状态（启用、禁用）
	 *
	 * @param questionDTO 编辑DTO
	 * @return 结果
	 */
	@ApiOperation(value = "编辑试题状态（启用、禁用）", notes = "必填字段：questionIdList、status")
	@PostMapping("/editStatus")
	public ApiResult<String> editStatus(@Validated({QuestionDTO.Delete.class}) @RequestBody QuestionDTO questionDTO) {
		questionService.editStatus(questionDTO);
		return new ApiResult<>("ok");
	}

	/**
	 * 获取试题题目列表
	 *
	 * @param questionDTO 试题DTO
	 * @return 结果
	 */
	@ApiOperation(value = "获取试题题目列表", notes = "必填：questionId")
	@PostMapping("/itemList")
	public ApiResult<QuestionDetailVO> getItemList(@Validated({QuestionDTO.Detail.class}) @RequestBody QuestionDTO questionDTO) {
		return ApiResult.success(questionService.getItemList(questionDTO));
	}

	/**
	 * 新建编辑试题题目
	 *
	 * @param detailDTO 试题明细DTO
	 * @return 结果
	 */
	@ApiOperation(value = "新建编辑试题题目", notes = "批量新建、编辑")
	@PostMapping("/itemList/update")
	public ApiResult<String> updateItemList(@Validated({QuestionDetailDTO.Edit.class}) @RequestBody QuestionDetailDTO detailDTO) {
		questionService.updateItemList(detailDTO);
		return new ApiResult<>("ok");
	}

	/**
	 * 删除试题题目
	 *
	 * @param delDTO 删除试题题目
	 * @return 结果
	 */
	@ApiOperation(value = "删除试题题目")
	@PostMapping("/itemList/delete")
	public ApiResult<String> deleteItemList(@Validated({QuestionItemDelDTO.Delete.class}) @RequestBody QuestionItemDelDTO delDTO) {
		questionService.deleteItemList(delDTO);
		return new ApiResult<>("ok");
	}

}

